package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.BusinessScenariosAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 业务场景属性联动处理类
 * @author huangjing
 *
 */
@Service
@AttrHandler(BusinessScenariosAttrHandler.class)
public class BusinessScenariosAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

	@Override
	public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
//		KiteInst kiteInst = querySceneInstService
//	            .getKiteInst(data.getCustId(), InstType.PROD_INST, attr.getInstId());
		List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(),inst.getInstId());
		// ProductCache.get(prodInstFunc.getProdId()).getProdNbr()
		if (KiteListUtils.isNotEmpty(prodInstFuncs)) {
			for (KiteProdInstFunc kiteProdInstFunc : prodInstFuncs) {
				if (KiteStringUtils.equalsAny(ProductCache.get(kiteProdInstFunc.getProdId()).getProdNbr(), "300000151", LocalKeyConsts.FUNC_PROD_ID_YF5GTX)) {
					 KiteAttr defaultCommunicationPlanAttr = kiteProdInstFunc.getAttr("200004023");
					 if (Objects.isNull(defaultCommunicationPlanAttr)) {
				            return;
                    }
                    IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
                    if (KiteStringUtils.isNotEmpty(attr.getValue())) {
                        if ("100001822".equals(attr.getValue())) { //B2B2C
                            defaultCommunicationPlanAttr.setIsNull(KeyConsts.IFTRUE_F);
                            operateSceneInstService
                                .setAttrs(data.getSceneInstId(), kiteProdInstFunc.getInstType(), kiteProdInstFunc.getInstId(), kiteProdInstFunc.listAttrs());
                        }
                        if ("100001821".equals(attr.getValue())) { //B2B
                            defaultCommunicationPlanAttr.setIsNull(KeyConsts.IFTRUE_T);
                            operateSceneInstService
                                .setAttrs(data.getSceneInstId(), kiteProdInstFunc.getInstType(), kiteProdInstFunc.getInstId(), kiteProdInstFunc.listAttrs());

                        }
                    }
				}
			}
		}

	}

	@Override
	public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
		// TODO Auto-generated method stub

	}

}
